import { _decorator, Component, Node, Sprite, EventTouch, Vec2, NodeEventType, CCFloat } from 'cc';
import { DataMgr } from '../../FrameWork/Data/DataMgr';
import { CardData } from '../../FrameWork/Common/CommonInterface';
import { PlantCardDtMgr } from '../../FrameWork/Data/DataBaseMgr';
import { SunMgr } from '../Mgr/SunMgr';
import { ModuleMgr } from '../ModuleMgr';
const { ccclass, property } = _decorator;

// 冷却中 缺少阳光 准备好了
export enum CardType {
    Cooling,
    WaitSun,
    Ready
}

@ccclass('Card')
export class Card extends Component {

    @property(Node)
    cardLight: Node = null;
    @property(Node)
    cardGray: Node = null;
    @property(Node)
    cardMask: Node = null;
    @property(CCFloat)
    cardID: number = 0;

    private _coolTimer: number = 0;
    private _cardMaskSprite: Sprite = null;
    // 卡片当前的状态
    private _currentState: CardType = CardType.Cooling;
    // 是否处在冷却
    private _isCooling: boolean = true;
    // 该卡片所需要的阳光数量
    private _cardNeedSun: number = 0;
    // 该卡片需要冷却的时间
    private _coolTime: number = 0;
    private _sunMgr: SunMgr = null;
    private _currentCardData: CardData = null;

    protected start(): void {
        this._sunMgr = ModuleMgr.instance.getModule("SunMgr") as SunMgr;
        this._init();
        this._loadCardData();
    }

    private _init() {

        this._cardMaskSprite = this.cardMask.getComponent(Sprite);
        this._cardMaskSprite.type = Sprite.Type.FILLED;
        this._cardMaskSprite.fillType = Sprite.FillType.VERTICAL;
        this._cardMaskSprite.fillStart = 1;
        this._cardMaskSprite.fillRange = -1;

        // 添加点击事件
        this.node.on(Node.EventType.TOUCH_END, this._isClickCard, this);
    }

    // 到时候更改卡片的图片 更新冷却时间与json文件同步
    private _loadCardData() {

        let cardDatas = DataMgr.instance.getMgr<PlantCardDtMgr>("PlantCardDtMgr").getAllDatas() as CardData[];
        for (let data of cardDatas) {
            if (data.ID === this.cardID) {
                this._currentCardData = data;
                this._cardNeedSun = data.Sun;
                this._coolTime = data.CoolDownTime;
                console.log(data);
                break;
            }
        }
    }

    private changeCardState(state: CardType) {

        if (this._currentState === state) {
            return;
        }
        switch (state) {
            // 冷却中 显示遮罩 开始计时
            case CardType.Cooling:
                this.cardMask.active = true;
                this._isCooling = true;
                break;
            case CardType.WaitSun:
                this.cardGray.active = true;
                this._isCooling = false;
                break;
            case CardType.Ready:
                this.cardLight.active = true;
                this.cardGray.active = false;
                this.cardMask.active = false;
                this._isCooling = false;
                break;
        }
        this._currentState = state;
    }

    // 开始冷却
    private _startCooling() {
        if (this._coolTime > 0) {
            this._coolTimer = 0;
            this.changeCardState(CardType.Cooling);
        }
    }

    // 检查阳光是否足够
    private _checkSunEnough() {

        let isEnoughSun = this._sunMgr.isEnoughSun(this._cardNeedSun);
        // 如果有充足的阳光并且不在冷却中 更新卡片状态
        if (isEnoughSun && !this._isCooling) {
            this.changeCardState(CardType.Ready);
        }
        // 如果没有阳光 显示灰色卡片
        else if (!isEnoughSun && !this._isCooling) {
            this.changeCardState(CardType.WaitSun);
        }
        return isEnoughSun;
    }

    // 满足阳光充足和冷却时间完毕可以点击
    private _isClickCard(event: EventTouch) {

        // 如果卡片在冷却中
        if (this._isCooling) {
            return;
        }
        // 检查阳光是否足够
        if (!this._checkSunEnough()) {
            return;
        }
        // 消耗阳光
        if (this._sunMgr.reduceSun(this._cardNeedSun)) {
            // 卡片开始冷却
            this._startCooling();
            // 种植逻辑
            console.log("开始种植" + this._currentCardData.Img);
        }
    }

    protected update(dt: number): void {
        // 增加冷却时间
        if (this._isCooling) {
            this._coolTimer += dt;
            // 更改冷却的进度
            let progress = -1 + this._coolTimer / this._coolTime;
            this._cardMaskSprite.fillRange = progress;

            // 冷却时间到了
            if (this._coolTimer >= this._coolTime) {
                this._isCooling = false;
                // 检查阳光数是否符合
                this._checkSunEnough();
            }
        }
        // 如果不在冷却中 检查阳光状态
        else {
            this._checkSunEnough();
        }
    }
}